//
//  iDashanAppDelegate.mm
//  iDashan
//
//  Created by Huahang Liu on 10/8/09.
//  Copyright University of Southern California 2009. All rights reserved.
//

#import "iDashanAppDelegate.h"
#import "MapViewController.h"
#import "FriendsViewController.h"
#import "LoginViewController.h"

@implementation iDashanAppDelegate

@synthesize window;
@synthesize username;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    
    //
    // Friends Database creation
    // 
    friendsDb = [[FriendsDb alloc] init];
        
    //
    // UI framework hook up
    // 
    // Create navigation controller for map tab
    mapTabNavigationController = [[UINavigationController alloc] init];
    mapTabNavigationController.title = @"Map";
    mapTabNavigationController.tabBarItem.image = [UIImage imageNamed:@"map.png"];
    mapTabNavigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    mapTabNavigationController.navigationBar.hidden = FALSE;
    // create map view controller
    MapViewController* mapViewController = [[[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil] autorelease];
    mapViewController.friendsDb = friendsDb;
    mapViewController.title = @"Map";
    [mapTabNavigationController pushViewController:mapViewController animated:YES];
    // Create navigation controller for friends tab
    friendsTabNavigationController = [[UINavigationController alloc]  init];
    friendsTabNavigationController.title = @"Friends";
    friendsTabNavigationController.tabBarItem.image = [UIImage imageNamed:@"friends.png"];
    friendsTabNavigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    friendsTabNavigationController.navigationBar.hidden = FALSE;
    // FriendsViewController
    FriendsViewController* friendsViewController = [[[FriendsViewController alloc] initWithNibName:@"FriendsViewController" bundle:nil] autorelease];
    friendsViewController.friendsDb = friendsDb;
    friendsViewController.title = @"Friends";
    [friendsTabNavigationController pushViewController:friendsViewController animated:YES];
    // Tab bar setup
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:mapTabNavigationController, friendsTabNavigationController, nil];
    [window addSubview:tabBarController.view];
    
    //
    // Some global settings
    //    
    // Main window setup
    [window makeKeyAndVisible];    
    // Set the status bar style
    [application setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:YES];
    
    //
    self.username = [NSString stringWithFormat:@"default"];
    
    LoginViewController* loginViewController = [[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil] autorelease];
    loginViewController.delegate = self;
    [mapTabNavigationController presentModalViewController:loginViewController animated:YES];
}

- (void)loginViewController:(LoginViewController*)controller didFinishLoginWithInfo:(NSDictionary*)loginInfo
{
    self.username = [loginInfo valueForKey:@"username"];
}


- (void)dealloc {
    [mapTabNavigationController release];
    mapTabNavigationController = nil;
    [friendsTabNavigationController release];
    friendsTabNavigationController = nil;
    [tabBarController release];
    tabBarController = nil;
    [window release];
    window = nil;
    [friendsDb release];
    friendsDb = nil;
    [username release];
    username = nil;
    [super dealloc];
}


@end
